package com.dab.chat.session;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dab.chat.R;
import com.dab.chat.bean.SessionListBean;
import com.dab.chat.ui.RoundImageView;
import com.dab.chat.utils.ImageLoadUtil;
import com.dab.chat.utils.TimeUtil;

import java.util.List;

/**
 * Created by dab on 2018/3/10 13:11
 */

public class SessionListAdapter extends RecyclerView.Adapter<SessionListAdapter.ViewHolder> {
    private List<SessionListBean> sessionListBeans;

    public void setSessionListBeans(List<SessionListBean> sessionListBeans) {
        this.sessionListBeans = sessionListBeans;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_itme_session_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SessionListBean sessionListBean = sessionListBeans.get(position);
        ImageLoadUtil.load(holder.mRoundImageView, sessionListBean.getPortrait(), R.drawable.chat_portrait);
        holder.mTvName.setText(sessionListBean.getName());
        holder.mTvContent.setText(sessionListBean.getContent());
        holder.mTvTime.setText(TimeUtil.getTimeDDMM(sessionListBean.getTime()));
        if (sessionListBean.getUnread() <= 0) {
            holder.mTvNum.setVisibility(View.GONE);
        } else {
            holder.mTvNum.setVisibility(View.VISIBLE);
            holder.mTvNum.setText("" + sessionListBean.getUnread());
        }

    }

    @Override
    public int getItemCount() {
        return sessionListBeans == null ? 0 : sessionListBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RoundImageView mRoundImageView;
        TextView mTvName;
        TextView mTvContent;
        TextView mTvTime;
        TextView mTvNum;

        public ViewHolder(View itemView) {
            super(itemView);
            mRoundImageView = itemView.findViewById(R.id.riv_chat_portrait);
            mTvName = itemView.findViewById(R.id.tv_chat_name);
            mTvContent = itemView.findViewById(R.id.tv_chat_content);
            mTvTime = itemView.findViewById(R.id.tv_chat_time);
            mTvNum = itemView.findViewById(R.id.tv_num);
        }
    }
}
